<?php
class FightAction extends Action {
	public function index(){
		$char = D("Charview");
		$cid1 = Session::get('cid');
		$cid2 = $_GET['targetid'];
		$char1 = $char->find("charbase.cid='$cid1' and charplus.cid=charbase.cid");
		$char2 = $char->find("charbase.cid='$cid2' and charplus.cid=charbase.cid");
		$zhandoujilu = $this->zhandou($char1, $char2);
		$this->assign('zhandoujilu',$zhandoujilu);
		$this->display();
	}


	/**
	 * 命中率计算
	 *
	 * @param $level1 进攻者等级
	 * @param $level2 防御者等级
	 * @param $mingzhong 进攻者命中
	 * @param $shanbi 防御者闪避
	 */
	public function mingzhonglv($level1, $level2, $mingzhong, $shanbi){
		$dengjichaju = $level1 - $level2;
		$dengjichaju > 10 ? $dengjichaju = 10 : ($dengjichaju < -10 ? $dengjichaju = -10 : $dengjichaju);
		$mingzhonglv = 55 + $mingzhong - $shanbi + 2 * $dengjichaju;
		$mingzhonglv > 95 ? $mingzhonglv = 95 : ($mingzhonglv < 10 ? $mingzhonglv = 10 : $mingzhonglv);
		return $mingzhonglv;
	}

	/**
	 * 战斗过程
	 *
	 * @param array $person1
	 * @param array $person2
	 */
	public function zhandou(array $person1, array $person2){
		//$person1打$person2的物理、魔法伤害和命中
		$wulishanghai1 = ($person1['wugong'] - $person2['wufang'])<0 ? 1 : $person1['wugong'] - $person2['wufang'];
		$mofashanghai1 = ($person1['mogong'] - $person2['mofang'])<0 ? 1 : $person1['mogong'] - $person2['mofang'];
		$mingzhonglv1 = $this->mingzhonglv($person1['level'],$person2['level'],$person1['mingzhong'],$person2['shanbi']);
		//$person2打$person1的物理、魔法伤害和命中
		$wulishanghai2 = ($person2['wugong'] - $person1['wufang'])<0 ? 1 : $person2['wugong'] - $person1['wufang'];
		$mofashanghai2 = ($person2['mogong'] - $person1['mofang'])<0 ? 1 : $person2['mogong'] - $person1['mofang'];
		$mingzhonglv2 = $this->mingzhonglv($person2['level'],$person1['level'],$person2['mingzhong'],$person1['shanbi']);
		$zhandoujilu = array();
		$lunshu = 1;
		while($lunshu <= 8){
			$i = 1;
			while ($i <= ceil($person1[gongsu]/33)){
				$damage = rand(0,100) <= $mingzhonglv1 ? ceil(($wulishanghai1 + $mofashanghai1)*rand(90,110)/100) : 0;
				$person2[nowhp] -= $damage;
				$jilu = $person1['name']."对".$person2['name']."造成了".$damage."伤害;=====".$person1['name']."血量:".$person1['nowhp'].";=====".$person2['name']."血量:".$person2['nowhp'];
				array_push($zhandoujilu,"$jilu");
				$i++;
				if($person2[nowhp] <= 0){
					break;
				}
			}
			if($person1[nowhp] <= 0 || $person2[nowhp] <= 0){
				break;
			}
			$i = 1;
			while ($i <= ceil($person2[gongsu]/33)){
				$damage = rand(0,100) <= $mingzhonglv2 ? ceil(($wulishanghai2 + $mofashanghai2)*rand(90,110)/100) : 0;
				$person1[nowhp] -= $damage;
				$jilu = $person2['name']."对".$person1['name']."造成了".$damage."伤害;=====".$person2['name']."血量:".$person2['nowhp'].";=====".$person1['name']."血量:".$person1['nowhp'];
				array_push($zhandoujilu,"$jilu");
				$i++;
				if($person1[nowhp] <= 0){
					break;
				}
			}
			if($person1[nowhp] <= 0 || $person2[nowhp] <= 0){
				break;
			}
			$lunshu++;
		}
		return $zhandoujilu;
		//dump($zhandoujilu);
	}
}
?>